<?php

namespace App\Jobs;

use App\Models\Clue;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class ClueCreatedJob implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $clue;
    public $timeout = 60;


    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Clue $clue)
    {
        $this->clue = $clue;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->clue) {
            $this->clue->checkCrmStatus();
            if (!$this->clue->no_dispatch && admin_setting('dispatch_enable'))
                $this->clue->mapToDispatchOfCheckRule();
            else {
                Log::info("不分配" ,[
                    'phone' => $this->clue->phone,
                ]);
            }
        }
    }

    /**
     * 解除任务唯一锁的秒数
     *
     * @var int
     */
    public $uniqueFor = 3600;

    /**
     * 任务的 unique ID (唯一ID)
     *
     * @return string
     */
    public function uniqueId()
    {
        return $this->clue->id;
    }
}
